package com.cmft.waltzpagemanager

import android.app.Activity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment

object ContainerManager {
    private const val TAG = "WaltzPageManager"

    @JvmStatic
    val pageList = mutableMapOf<String, IContainer>()

    /**
     * 默认页面App提供者，在App启动时赋值
     */
    @JvmStatic
    var pageProvider: IPageProvider? = null

    /**
     * 获取刷新用户信息
     */
    @JvmStatic
    var userManager: IUserManager? = null

    /**
     * 注册容器
     * @param container 容器对象
     */
    @JvmStatic
    fun registerContainer(container: IContainer) {
        if (pageList[container.getName()] != null) {
            Log.i(TAG, "registerContainer: ${container.getName()}已注册")
            return
        }
        pageList[container.getName()] = container
    }

    /**
     * 根据pageId和容器名称启动页面
     * @param activity 启动页面的context
     * @param pageId 页面
     * @param param 给页面的参数
     * @param containerName 容器名称
     * @param requestCode 默认值-1, 不等于-1时会调用startActivityForResult
     */
    @JvmStatic
    fun startPage(
        activity: Activity,
        pageId: String,
        containerName: String,
        param: Bundle? = null,
        requestCode: Int = -1
    ) {
        val wzPageContainer = pageList[containerName]
        wzPageContainer?.startPage(activity, pageId, param, requestCode)
    }

    /**
     * 根据pageApp启动页面
     * @param activity 启动页面的context
     * @param pageApp 页面信息
     * @param param 给页面的参数
     * @param requestCode 默认值-1, 不等于-1时会调用startActivityForResult
     */
    @JvmStatic
    fun startPage(
        activity: Activity,
        pageApp: PageApp,
        param: Bundle? = null,
        requestCode: Int = -1
    ) {
        val wzPageContainer = pageList[pageApp.containerName]
        wzPageContainer?.startPage(activity, pageApp, param, requestCode)
    }

    /**
     * 根据pageId获取fragment
     * @param pageId 页面Id
     * @param param 给页面的参数
     * @param containerName 容器名称
     */
    @JvmStatic
    fun getPageFragment(
        pageId: String,
        containerName: String,
        param: Bundle? = null
    ): Fragment? {
        val wzPageContainer = pageList[containerName]
        return wzPageContainer?.getPageFragment(pageId, param)
    }

    /**
     * 根据pageApp获取fragment
     * @param pageApp 页面信息
     * @param param 给页面的参数
     */
    @JvmStatic
    fun getPageFragment(
        pageApp: PageApp,
        param: Bundle? = null
    ): Fragment? {
        val wzPageContainer = pageList[pageApp.containerName]
        return wzPageContainer?.getPageFragment(pageApp, param)
    }
}